package com.frx.datastruct.chapterfour;

import com.frx.datastruct.chapterthree.Stack;

/**
 * @author ：frx
 * @date ：Created in 2019/2/18 21:03
 * @description： 使用链表实现的栈
 * @modified By：
 * @version: ToDO
 */
public class LinkedListStack<E> implements Stack<E> {

    private LinkedList<E> list;

    public LinkedListStack(){
        list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    /** 返回链表头的元素 */
    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append("Stack:top ");
        res.append(list);
        return res.toString();
    }

    public static void main(String[] args){
        LinkedListStack<Integer> listStack = new LinkedListStack<>();
        for(int i = 0;i<5;i++){
            listStack.push(i);
            System.out.println(listStack);
        }

        listStack.pop();
        System.out.println(listStack);
    }
}
